package com.palmshop.online.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.palmshop.online.dao.BasketDao;
import com.palmshop.online.entity.PalmResult;
import com.palmshop.online.entity.ShoppingCart;
import com.palmshop.online.service.BasketService;

/**
 * 购物车service实现类
 * @author quan
 *
 */
@Service
@Transactional
public class BasketServiceImpl implements BasketService {

	@Resource
	private BasketDao dao;

	/* >>>>>>>>>>>>>购物车>>>>>>>>>>>>>>>>>>>>>>>>>> */
	/**
	 * 向购物车表中插入一条新的数据
	 * 步骤：现根据商品的条形码和商铺的唯一性标识去数据库查询该商品是否存在-如果存在，修改数量，如果不存在，则
	 * 执行插入操作
	 * 
	 * @param shopBasket:购物车实体类
	 * @return result
	 */
	public PalmResult addBasket(ShoppingCart shopBasket) {
		PalmResult result = new PalmResult();

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("cusUnique", shopBasket.getCusUnique());
		map.put("goodsBarcode", shopBasket.getGoodsBarcode());
		map.put("shopUnique", shopBasket.getShopUnique());
		List<Map<String, Object>> list = dao.findShopCartById(map);
		
		if (list != null && list.size() > 0) {
			Integer shoppingCartCount = (Integer) list.get(0).get(
					"shoppingCartCount");
			Integer shoppingCartId = (Integer) list.get(0)
					.get("shoppingCartId");
			Map<String, Object> map2 = new HashMap<String, Object>();
			map2.put("shoppingCartCount",
					shoppingCartCount + shopBasket.getShoppingCartCount());
			map2.put("shoppingCartId", shoppingCartId);
			
			if (dao.updateShoppCartById(map2) > 0) {
				result.setStatus(1);
				result.setData(shoppingCartId);//将购物车id返回
			}

		} else {
			if (dao.addBasket(shopBasket) > 0) {
				result.setStatus(1);
				System.out.println("shoppingCartId:"+shopBasket.getShoppingCartId());
				result.setData(shopBasket.getShoppingCartId());
			}
		}

		return result;
	}

	/**
	 * 根据购物车id删除一条对应的信息
	 * 
	 * @param basketId:购物车id
	 * @return result
	 */
	public PalmResult deleteBasket(Integer basketId) {
		PalmResult result = new PalmResult();
		int del = dao.deleteBasket(basketId);
		if (del > 0) {
			result.setStatus(1);
		}
		return result;
	}

	/**
	 * 根据购物车id修改对应的商品数量
	 * 
	 * @param map
	 * @return result
	 */
	public PalmResult updateShoppCartById(Map map) {
		PalmResult result = new PalmResult();
		if (dao.updateShoppCartById(map) > 0) {
			result.setStatus(1);
		}
		return result;
	}

	/**
	 * 根据用户的Id查询对应的购物车信息
	 * 
	 * @param map
	 * @return result
	 */
	public PalmResult findBasketById(Map map) {
		PalmResult result = new PalmResult();
		List<Map<String, Object>> list = dao.findBasketById(map);
		if (list != null && list.size() > 0) {
			result.setStatus(1);
			result.setData(list);
		}
		return result;
	}
	
	/**
	 * 根据购物车id查询订单信息
	 * @param ids:购物车id数组
	 * @return result
	 */
	public PalmResult findBasketByIds(Integer[]ids){
		PalmResult result = new PalmResult();
		List<Map<String, Object>> list = dao.findBasketByIds(ids);
		if (list != null && list.size() > 0) {
			result.setStatus(1);
			result.setData(list);
		}
		return result;
	}

	/**
	 * 查询出当前购物车中商品的数量
	 * 
	 * @param map
	 * @return result
	 */
	public PalmResult findSumBasket(Map map) {
		PalmResult result = new PalmResult();
		result.setStatus(0);
		Integer count  = dao.findSumBasket(map);
		if (count!=null&&count>0) {
			result.setStatus(1);
			result.setData(count);
		}
		return result;

	}

	/* >>>>>end 购物车 end>>>>>>>>>>>>>>>>>>> */

}
